package com.yaowk.common.api.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OrderUtils {

    private static final SimpleDateFormat orderFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 生成订单号
     *
     * @return
     */
    public static String createOrderNo(Integer length) {
        int last = length - 14;
        byte[] bytes = new byte[last];
        for (int i = 0; i < last; i++) {
            bytes[i] = '0';
        }
        Integer nextInt = Integer.valueOf("1".concat(new String(bytes)));

        String res = String.valueOf(new Random().nextInt(nextInt));
        if (res.length() != last) {
            res = String.format("%" + last + "s", res);
            res = res.replaceAll("\\s", "0");
        }

        return orderFormat.format(new Date()) + res;
    }

    /**
     * 生成20位订单号
     *
     * @return
     */
    public static String createOrderNo() {
        return OrderUtils.createOrderNo(20);
    }
}
